<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img{
            width: 150px;
            
        }
    </style>
</head>
<body>
    <table id="all" ></table>
    <script src="./jquery-1.12.4.min.js"></script>
    <script>
    $('#all').on('click','.add',function(){
        var id = $(this).parents('tr').children().eq(0).text();
        $.ajax({
            url:'http://localhost/interface/update.php',
            data:{
                id:id,
                type:"add"
            },
            success:function(res){
                // console.log(res);
                if(res.code===1){
                    show();
                }
            },
            dataType:'json'
        })
    })
    //减少商品
    $("#all").on('click','.cut',function(){
        var id = $(this).parents('tr').children().eq(0).text();
        $.ajax({
            url:'http://localhost/interface/update.php',
            data:{
                id:id,
                type:'cut'
            },
            success:function(res){
                // console.log(res);
                if(res.code===1){
                    show();
                }
            },
            dataType:'json'
        })
    })
    //删除商品
    $('#all').on('click','.del',function(){
        var id = $(this).parents('tr').children().eq(0).text();
        $.ajax({
            url:'http://localhost/interface/del.php',
            data:{
                id:id
            },
            success:function(res){
                // console.log(res);
                if(res.code===1){
                    show()
                }
            },
            dataType:'json'
        })
    })



    show()
    function show(){
        $.ajax({
        url:'http://localhost/interface/showlist.php',
        success:function(res){
            // console.log(res);
            $('#all').html(`<tr>
                <th>编号</th>
                <th>商品名</th>
                <th>图片</th>
                <th>价格</th>
                <th>数量</th>
                <th>删除</th>
            </tr>`)
            if(res.code==1){
                res.data.forEach(function(val){
                    $('#all').append(`<tr>
                        <td>${val.product_id}</td>
                        <td style="width:200px;">${val.product_name}</td>
                        <td>
                            <img src="${val.product_img}" alt="">
                        </td>
                        <td style="color:red">
                            ${val.product_price}
                        </td>
                        <td>
                            <span class="cut">-</span>
                            <input type="text" value='${val.product_num}' disabled>
                            <span class="add">+</span>
                        </td>
                        <td>
                            <input type="button" value="删除" class="del">
                        </td>
                    </tr>`)
                    
                })
            }
        },
        dataType:'json'
    })
    }
    </script>
</body>
</html>